package com.ygo.feihua.fragment;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.net.*;
import android.os.*;
import android.support.v4.app.*;
import android.text.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import com.ygo.feihua.util.*;
import java.io.*;
import com.ygo.feihua.*;

public class FragmentImage extends Fragment implements OnClickListener
{
	private EditText ed_km;
	private Button bu_zz;
	private ImageView im_kt;
	private Gongju gj;
	private DialogUtils du;
	String sjk_path="";

	public void save()
	{
		if(!TextUtils.isEmpty(ed_km.getText().toString()))
		{
			final String card=sjk_path + "/pics/" + ed_km.getText().toString() .trim() + ".jpg";
			final Bitmap bm =((BitmapDrawable) im_kt.getDrawable()).getBitmap();

			if (new File(card).exists())
			{
				View[] v=du.dialogt(false,"",ed_km.getText().toString() .trim() + ".jpg已存在，是否覆盖?");
				Button b1,b2;
				b1=(Button) v[0];
				b2=(Button) v[1];
				b1.setText("取消");
				b2.setText("覆盖");
				b1.setOnClickListener(new OnClickListener(){

						@Override
						public void onClick(View p1)
						{
							du.dis();
							// TODO: Implement this method
						}
					});
				b2.setOnClickListener(new OnClickListener(){

						@Override
						public void onClick(View p1)
						{
							String ss=savePNG_After(bm,card);
							if(ss.equals("")){
								gj.show("已覆盖");
							}else{
								gj.show("覆盖失败,原因为"+ss);
							}
							// TODO: Implement this method
						}
					});
			}
			else
			{

				String ss=savePNG_After(bm,card);
				if(ss.equals("")){
					gj.show("已覆盖");
				}else{
					gj.show("覆盖失败,原因为"+ss);
				}
			}
		}
		else
		{
			gj.show( "卡密和图片不能为空");
		}
		
		// TODO: Implement this method
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_cardimage_information,null);
		initView(v);
		
		// TODO: Implement this method
		return v;
	}

	private void initView(View v)
	{
		ed_km = (EditText)v.findViewById(R.id.fai_km);
		bu_zz = (Button)v.findViewById(R.id.fai_zz);
		im_kt = (ImageView)v.findViewById(R.id.fai_kt);
		gj=Gongju.getdx(getActivity());

		sjk_path=SharedPreferenceUtil.getYgopath();
		du=DialogUtils.getdx(getActivity());

		
		im_kt.setOnClickListener(this);
		bu_zz.setOnClickListener(this);

		
		// TODO: Implement this method
	}

	

	public void onClick(View p1)
	{
		switch (p1.getId())
		{
			case R.id.fai_kt:
				{
					Intent intent=new Intent();
					intent.putExtra("lx",1);
					intent.putExtra("path",Gongju.PATH_GEN);
					intent.setClass(getActivity(), NewWjxz.class);
					intent.putExtra("过滤", ".jpg");
					startActivityForResult(intent, 0);
					break;
				}
			case R.id.fai_zz:
				{
					Intent i = IntentUtil.getAppIntent(getActivity(),"com.legendhan.mose2"); 
					if (i != null)
					{
						startActivity(i); 
					}else{
						gj.show("制作属于自己的游戏王卡片——MoSE");
						Intent intent=new Intent(Intent.ACTION_VIEW);
						intent.setData(Uri.parse("http://www.wandoujia.com/apps/com.legendhan.mose"));
						startActivity(intent);
					}
					break;
				}
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		
		//获取返回的值
		String result= data.getStringExtra("path");
		if (!result.equals(""))
		{
			
			Bitmap bm=BitmapFactory.decodeFile(result);
			im_kt.setImageBitmap(bm);
		}
		
	}
	

	public void setKt(int type,String km){
		if(km!=null&&!km.equals("")){
			switch(type){
				case CardImageUtil.TYPE_KM:
					ed_km.setText(km);
					setkt(km);
		
					break;
				case CardImageUtil.TYPE_PATH:
					String s=km.substring(km.indexOf("/")+1,km.indexOf("."));
					ed_km.setText(s);
					setkt(s);
					break;
			}
		}
	}
	
	Handler han=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case 0:
					im_kt.setImageBitmap((Bitmap)msg.obj);
					break;
			}
		}
	};
	
	private void setkt(final String km){
		Gongju.setKt(getActivity(),im_kt,km);
	}
	
	public static String savePNG_After(Bitmap bitmap, String name) {
		File file = new File(name);
		try {
			FileOutputStream out = new FileOutputStream(file);
			if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
				out.flush();
				out.close();
			}
			return "";
		} catch (FileNotFoundException e) {
			return e+"";
		} catch (IOException e) {
			return e+"";
		}
	}
	
}
